import config from '@/config/config.js';
import axios from 'axios';

const itemTypeRequest = {

    /**
     * 更新物料信息
     */
    EditDateSubmit(itemType, callback) {
        const url = `${config.mdBaseUrl}/itemtype/update`;

        axios.post(url, itemType, {
            headers: {
                token: localStorage.getItem("token")
            }
        }).then(res => {
            callback(res.data);
        }).catch(err => {
            console.error("请求失败:", err);
        });
    },

    /**
     * 加载所有物料类型列表
     * @param searchItemType 查询条件
     * @param callback 回调函数
     */
    loadItemTypeList(searchItemType, callback) {
        const url = `${config.mdBaseUrl}/itemtype/getList`;

        axios.get(url, {
            params: {
                itemTypeName: searchItemType.itemTypeName || null,
                enableFlag: searchItemType.enableFlag !== null ? searchItemType.enableFlag : null,
                ancestors: searchItemType.ancestors || 0,
                parentId: searchItemType.parentId || null
            },
            headers: {
                token: localStorage.getItem("token")
            }
        }).then(res => {
            callback(res.data);
        }).catch(err => {
            console.error("请求失败:", err);
        });
    },

    /**
     * 通过id查询物料类型信息
     * @param searchItemType
     * @param callback
     */
    loadUpdateInfo(searchItemType, callback) {
        const url = `${config.mdBaseUrl}/itemtype/getListById`;

        axios.get(url, {
            params: {
                itemTypeId: searchItemType.itemTypeId || null
            },
            headers: {
                token: localStorage.getItem("token")
            }
        }).then(res => {
            callback(res.data);
        }).catch(err => {
            console.error("请求失败:", err);
        });
    },

    saveItemType(itemType, callback) {
        const url = `${config.mdBaseUrl}/itemtype/save`;

        axios.post(url, itemType, {
            headers: {
                token: localStorage.getItem("token")
            }
        }).then(res => {
            callback(res.data);
        }).catch(err => {
            console.error("请求失败:", err);
        });
    },

    /**
     * 判断是否是父级物料类型
     * @param itemTypeId
     * @param callback
     */
    isParentItemType(itemTypeId, callback) {
        const url = `${config.mdBaseUrl}/itemtype/isParent?itemTypeId=${itemTypeId}`;

        axios.get(url, {
            headers: {
                token: localStorage.getItem("token")
            }
        }).then(res => {
            callback(res.data);
        }).catch(err => {
            console.error("请求失败:", err);
        });
    },

    deleteItemType(itemTypeId, callback){
        const url = `${config.mdBaseUrl}/itemtype/del?itemTypeId=${itemTypeId}`;
        //使用逻辑删除put请求
        axios.delete(url,{
            headers:{
                token:localStorage.getItem("token")
            }
        }).then(res=>{
            callback(res.data);
        }).catch(err=>{
            console.error("请求失败:",err);
        })
    }
};

export default itemTypeRequest;
